<?php 
/**
 * A class to handle the Mixi Echo service
 */
 class MixiEcho extends MixiCommon
 {
 	public function __construct($auth_obj, $auth_method)
 	{
 		parent::__construct($auth_obj, $auth_method);
 	}
 	
 	public function getEchos($filterManager = null)
 	{
 		$echos = array();
 		
 		for($i = 1; $i <= MAX_PAGES; $i++)
 		{
 			$echo_page = $this->getEchoPage($i, $filterManager);
 			
 			// Keep merging with the other echo pages
 			if($echo_page)
 			{
 				$echos = array_merge($echos,$echo_page);
 			}
 			// No echo results for that page, or no results at all
 			else
 			{
 				return $echos;
 			}
 			
 			// The more you ask for, the more you wait :)
 			$sleep = $i * 0.25 * 1000000;
 			
 			usleep($sleep);
 		}	
 		
 		return $echos;
 	}
 	
 	/**
 	 * Retrieve a page of echos 
 	 * 
 	 * @param $filterManager object This will be an object to handle filtering of data.
 	 * 
 	 * @return array - An associative array of echos in the form<br/>
 	 * nickname => nickname of the user making the comment<br/>
 	 * replyto => (optional) who the comment is directed to<br/>
 	 * posted => What time (Japan) the comment was posted [php timestamp]<br/>
 	 * comment => the comment made<br/>
 	 * selfnick => Your own nickname<br/>
 	 */
 	public function getEchoPage($page,$filterManager=null)
 	{			
 		$html = $this->request("http://mixi.jp/recent_echo.pl?page={$page}");
 		$this->parser->loadHTML($html);
 		$xpath = new DOMXpath($this->parser);
 		$elements = $xpath->query("//td[@class='comment']"); #This holds the echo data for each user
 		
 		// This will probably occur if you're not using echo, or using it for the first time
 		if(!$elements)
 			return array();

		// Get our own nickname dynamically since we could change it
		// We'll pass this to every page request so that if for whatever reason
		// our nickname gets changed while the app is running, it will look correct with the filters
		$nickname_element = $xpath->query("//div[@class='thumb']/span");
		if($nickname_element)
		{
			$selfnick = $nickname_element->item(0)->nodeValue;
		}
		else
		{
			$selfnick = '';
		}
 		
 		$echos = array();

 		foreach($elements as $element)
 		{		
 			$echo = array('nickname' => '', 'replyto' => '', 'comment' => '', 'posted' => '', 'selfnick' => $selfnick);
 				
 			// This searches for replies to other users
 			$links = $xpath->evaluate("a", $element);
 			foreach($links as $link)
 			{
 				if(strstr($link->getAttribute('href'), 'view_echo.pl?from=nickname'))
 				{
 					$echo ['replyto'] = $link->nodeValue;
 				}
 			}
 			
 			// The divs with ids in them have nickname and other information
 			$echo_divs = $xpath->evaluate("div[@id]", $element);
 			foreach($echo_divs as $div)
 			{
 				if(strstr($div->getAttribute('id'), "nickname"))
 				{					
 					$echo['nickname'] = $div->nodeValue;
					$filter = true; 					
 				}
 				else if(strstr($div->getAttribute('id'), "body"))
 				{
 					$echo['comment'] = $div->nodeValue;
 				}
 				else if(strstr($div->getAttribute['id'], "post_time"))
 				{
 					// So first, regex out the times from this compressed string
 					// Format: [YEAR][MONTH][DAY][HOUR][MINUTE][SECOND]
 					$regex = "/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/";
 				}
 			}
 			
 			if($filterManager)
 			{
 				/** 
 				 * Here the filter manager applies all filters. This could be
 				 * search filters, word replacements, etc.  A filter manager needs
 				 * only to have an applyFilters function and return the echo in the
 				 * same format as described in getEchoList() 
 				 */
 				$filteredEcho = $filterManager->applyFilters($echo);
 				
 				// The opposite is a negative filter, so it doesn't get shown
 				if($filteredEcho)
 				{
 					$echos[] = $filteredEcho;
 				}
 			}
 			else
 			{
 				$echos[] = $echo;
 			}
 		}
 		
 		return $echos;
 	}
 }
?>
